package com.credit.jumu.playcard.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.credit.jumu.playcard.R;
import com.credit.jumu.playcard.base.BaseActivity;
import com.credit.jumu.playcard.utils.GlideUtil;
import com.credit.jumu.playcard.utils.ToastUtil;
import com.credit.jumu.playcard.view.ChooseCardDialog;
import com.credit.jumu.playcard.view.ChooseImageDialog;
import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.common.ImageLoader;
import com.yuyh.library.imgsel.config.ISCameraConfig;
import com.yuyh.library.imgsel.config.ISListConfig;

import java.util.List;

import static android.R.attr.path;

/**
 * Created by Administrator on 2017/10/19/019.
 */

public class RealNameActivity extends BaseActivity implements View.OnClickListener, ChooseImageDialog.OnClickCallBack {
	ImageView imageIcon;
	ImageView titleback;
	TextView title;
	ImageView realicon1, realicon2, realicon3, ivreal1, ivreal2, ivreal3;
	TextView realtext1, realtext2, realtext3, realok;
	public final int REQUEST_CAMERA_CODE = 1001;//相机
	public final int REQUEST_LIST_CODE = 1002;//相册
	public ChooseImageDialog chooseImageDialog;
	int chooseIndex = 0;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_realname);
		// 自定义图片加载器
		ISNav.getInstance().init(new ImageLoader() {
			@Override
			public void displayImage(Context context, String path, ImageView imageView) {
				Glide.with(context).load(path).into(imageView);
			}
		});
		chooseImageDialog = new ChooseImageDialog(this, this);
		initview();

	}

	private void initview() {
		imageIcon = (ImageView) findViewById(R.id.imageIcon);
		imageIcon.setVisibility(View.GONE);
		titleback = (ImageView) findViewById(R.id.titleback);
		titleback.setVisibility(View.VISIBLE);
		title = (TextView) findViewById(R.id.title);
		title.setText("实名认证");

		realicon1 = (ImageView) findViewById(R.id.realicon1);
		realicon2 = (ImageView) findViewById(R.id.realicon2);
		realicon3 = (ImageView) findViewById(R.id.realicon3);
		realtext1 = (TextView) findViewById(R.id.realtext1);
		realtext2 = (TextView) findViewById(R.id.realtext2);
		realtext3 = (TextView) findViewById(R.id.realtext3);
		ivreal1 = (ImageView) findViewById(R.id.ivreal1);
		ivreal2 = (ImageView) findViewById(R.id.ivreal2);
		ivreal3 = (ImageView) findViewById(R.id.ivreal3);
		realok = (TextView) findViewById(R.id.realok);
		realok.setOnClickListener(this);
		realicon1.setOnClickListener(this);
		realicon2.setOnClickListener(this);
		realicon3.setOnClickListener(this);
		backClick(R.id.titleback, RealNameActivity.this);
	}

	private void realNameOk() {
		realicon1.setImageResource(R.mipmap.realok);
		realicon2.setImageResource(R.mipmap.realok);
		realicon3.setImageResource(R.mipmap.realok);
		realtext1.setText("已认证");
		realtext2.setText("已认证");
		realtext3.setText("已认证");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.realok:
				realNameOk();
				break;
			case R.id.realicon1:
				chooseIndex = 1;
				if (null != chooseImageDialog) {
					chooseImageDialog.showDialog();
				}
				break;
			case R.id.realicon2:
				chooseIndex = 2;
				if (null != chooseImageDialog) {
					chooseImageDialog.showDialog();
				}
				break;
			case R.id.realicon3:
				chooseIndex = 3;
				if (null != chooseImageDialog) {
					chooseImageDialog.showDialog();
				}
				break;
			case R.id.titleback:
				this.finish();
				break;

		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == REQUEST_CAMERA_CODE && resultCode == RESULT_OK && data != null) {
			String path = data.getStringExtra("result"); // 图片地址
			if (chooseIndex == 1) {
				GlideUtil.loadNotMemoryImage(this, path, ivreal1);
			} else if (chooseIndex == 2) {
				GlideUtil.loadNotMemoryImage(this, path, ivreal2);
			} else if (chooseIndex == 3) {
				GlideUtil.loadNotMemoryImage(this, path, ivreal3);
			}
			ToastUtil.showShortToast(path);

		} else if (requestCode == REQUEST_LIST_CODE && resultCode == RESULT_OK && data != null) {
			List<String> pathList = data.getStringArrayListExtra("result");
			for (String path : pathList) {
				ToastUtil.showShortToast("相册" + path);
				if (chooseIndex == 1) {
					GlideUtil.loadNotMemoryImage(this, path, ivreal1);
				} else if (chooseIndex == 2) {
					GlideUtil.loadNotMemoryImage(this, path, ivreal2);
				} else if (chooseIndex == 3) {
					GlideUtil.loadNotMemoryImage(this, path, ivreal3);
				}
			}
		}
	}

	//开启相机
	public void imgToCamera() {
		ISCameraConfig config = new ISCameraConfig.Builder()
				.needCrop(true) // 裁剪
				.cropSize(1, 1,300, 600)
				.build();
		ISNav.getInstance().toCameraActivity(this, config, REQUEST_CAMERA_CODE);
	}

	//从相册中选
	public void imgToAlbum() {
		// 自由配置选项
		ISListConfig config1 = new ISListConfig.Builder()
				// 是否多选, 默认true
				.multiSelect(false)
				// 是否记住上次选中记录, 仅当multiSelect为true的时候配置，默认为true
				.rememberSelected(false)
				// “确定”按钮背景色
				.btnBgColor(Color.GRAY)
				// “确定”按钮文字颜色
				.btnTextColor(Color.BLUE)
				// 使用沉浸式状态栏
				.statusBarColor(Color.parseColor("#3F51B5"))
				// 返回图标ResId
				.backResId(android.support.v7.appcompat.R.drawable.abc_ic_ab_back_mtrl_am_alpha)
				// 标题
				.title("图片")
				// 标题文字颜色
				.titleColor(Color.WHITE)
				// TitleBar背景色
				.titleBgColor(Color.parseColor("#3F51B5"))
				// 裁剪大小。needCrop为true的时候配置
				.cropSize(1, 1, 300, 600)
				.needCrop(true)
				// 第一个是否显示相机，默认true
				.needCamera(false)
				// 最大选择图片数量，默认9
				.maxNum(1)
				.build();
		// 跳转到图片选择器
		ISNav.getInstance().toListActivity(this, config1, REQUEST_LIST_CODE);
	}

	@Override
	public void onChooseCamera() {
		imgToCamera();
		ToastUtil.showShortToast("打开相机");
		chooseImageDialog.cancelDialog();
	}

	@Override
	public void onChoosePhoto() {
		imgToAlbum();
		ToastUtil.showShortToast("打开相册");
		chooseImageDialog.cancelDialog();
	}
}
